<?php
require_once 'db.php';

class Article
{
    private $aid;     // user id
    private $fields;  // other record fields

    // initialize a User object
    public function __construct()
    {
        $this->aid = null;
        $this->fields = array('title' => '',
                              'keywords' => '',
                              'articleDesc' => '',
                              'content' => '',
        					  'issueDate'=>'');
    }

    // override magic method to retrieve properties
    public function __get($field)
    {
        if ($field == 'aid')
        {
            return $this->aid;
        }
        else 
        {
            return $this->fields[$field];
        }
    }

    // override magic method to set properties
    public function __set($field, $value)
    {
    	if($field=='aid'){
    		$this->aid=$value;
    	}
        elseif (array_key_exists($field, $this->fields))
        {
            $this->fields[$field] = $value;
        }
    }

    // return an object populated based on the record's user id
    public static function getById($aid)
    {
        $article = new Article();

        $query = sprintf('SELECT title, keywords, articleDesc, content ' .
            'FROM Article WHERE id = %d',
            $aid);
        $result = mysql_query($query, $GLOBALS['DB']);

        if (mysql_num_rows($result))
        {
        	
            $row = mysql_fetch_assoc($result);
            $article->title = $row['title'];
            $article->keywords = $row['keywords'];
            $article->articleDesc = $row['articleDesc'];
            $article->content = $row['content'];
            $article->aid = $aid;
        }
        mysql_free_result($result);

        return $article;
    }

    // save the record to the database
    public function save()
    {
        if ($this->aid)
        {
            $query = sprintf('UPDATE %sArticle SET title = "%s", ' .
                'keywords = "%s", articleDesc = "%s", content ="%s" ,issueDate="%s" ' .
                'WHERE USER_ID = %d',
                DB_TBL_PREFIX,
               mysql_real_escape_string($this->title, $GLOBALS['DB']),
                mysql_real_escape_string($this->keywords, $GLOBALS['DB']),
                mysql_real_escape_string($this->articleDesc, $GLOBALS['DB']),
                mysql_real_escape_string($this->content, $GLOBALS['DB']),
                date('c'),
                $this->aid);
            mysql_query($query, $GLOBALS['DB']);
        }
        else
        {
            $query = sprintf('INSERT INTO %sArticle (title, keywords, ' .
                'articleDesc, content,issueDate) VALUES ("%s", "%s", "%s", "%s","%s")',
                DB_TBL_PREFIX,
                mysql_real_escape_string($this->title, $GLOBALS['DB']),
                 mysql_real_escape_string($this->keywords, $GLOBALS['DB']),
                mysql_real_escape_string($this->articleDesc, $GLOBALS['DB']),
                mysql_real_escape_string($this->content, $GLOBALS['DB']),
                date('c')
              );
            mysql_query($query, $GLOBALS['DB']);

            $this->aid = mysql_insert_id($GLOBALS['DB']);
        }
    }
}

function listArticles($start,$end){
	
	$query="select p.id,p.title ,p.keywords,p.issueDate,p.articleDesc from Article p order by p.issueDate desc LIMIT ".$start.",".$end;
	$result = mysql_query($query, $GLOBALS['DB']);

	$articles=array();
	$num=mysql_num_rows($result);
	for($i = 0;$i<$num; $i++){
		$row=mysql_fetch_assoc($result);
		$sp=new Article();
		$sp->title=$row['title'];
		$sp->aid=$row['id'];
		$sp->keywords=$row['keywords'];
		//$sp->content=$row['content'];
		$sp->articleDesc=$row['articleDesc'];
		$sp->issueDate=$row['issueDate'];
		$articles[$i]=$sp;
	}
	mysql_free_result($result);
	return $articles;
	
}

function buildArticlesHtml($prodctList){
	echo "<div class=produces2>\n";
	foreach ($prodctList as $product){
		echo "<div class=produce onmousemove=\"this.className='produce2'\" onmouseout=\"this.className='produce'\">\n";
		echo "<div class=p_pic><a href='/product/$product->pid.html'><img src=images/$product->produceImg width='120' height='120' /></a></div>";
		echo "<div class='dec'><a href='/product/$product->pid.html'>$product->pName</a></div>\n";
		echo "<div class='price'>$product->price</div>\n";
		echo "</div>\n";
	}
	echo "</div>\n";
}
function buildArticleHtml($artilce){
	echo "<div class=artice>\n";
	echo "<h2>$artilce->title</h2>";
	echo "<div class=produce_intro>";
		echo "	<img src='images/produce_b.jpg' width='240' height='240'/>"	;
		echo "	<input type=button class=btn_buy onmousemove=\"this.className='btn_buy2'\" onmouseout=\"this.className='btn_buy'\" value='' />";
	echo "</div>";
	echo "<div>$artilce->content</div>";
	echo"</div>";
}

?>